import { Component,useEffect,useState } from 'react'
import { View, Text,Input,Icon,Swiper,SwiperItem,Image,ScrollView } from '@tarojs/components'
import './index.less'
import Taro from '@tarojs/taro'
import { getSortBlog } from '../../api/sort'
import { getBlog } from '../../api/blog'
export default function SortBlog (props){
  const [data,setDate] = useState([])
  const [sort,setSort] = useState('')
  useEffect(()=>{
    if(props.data.sort_list){
      let list =[]
      let listdata = []
      getSortBlog(props.data.sort_id).then(res=>{
        setSort(props.data.sort_name)
        res.data.data.length?res.data.data.map(item => {
          list.push(item.sort_list)
        }):''
        getBlog().then(res => {
          res.data.data.map(item => {
            list.map(it => {
              if(it[0].user_id == item.user_id&&it[0].blog_id==item.blog_id)listdata.push(item)
            })
          })
          setDate(listdata)
        })
      })
    }
  },[props.data])

  const getDate = (date) => {
    date = new Date(date)
    return date.getFullYear()+'年'+(date.getMonth()+1)+'月'+date.getDate()+'日'
  }
  return (
    <ScrollView
      scrollY
      scrollWithAnimation
      className='content'>
      {data.map(item => (
            <View className="card" onClick={()=>{Taro.navigateTo({ url: '/pages/detail/index?blog_id='+item.blog_id })}}>
            <View className='title'>{item.title}</View>
            <View className='content'>{item.content}</View>
            <View className='info'>博客 | {item.user_data?item.user_data[0].nickname:''} {getDate(item.time)}</View>
          </View>))}
    </ScrollView>
  )
}